//Documentation complete
/**
 * @author Jason
 * @version 2009MR02
 */

package ui.internal.ajax;

import problemDomain.*;
import control.*;
import static problemDomain.ErrorBean.*;
import static utilities.XMLTools.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 * Servlet implementation class MaintainOrder
 */
public class MaintainOrder extends HttpServlet {
	
	/* Constants */
	
	private static final long serialVersionUID = -6000424672898369095L;
	
	/* END Constants */
	
	
	/* (HttpServlet) Methods */
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		response.setContentType("text/xml");
		if (session != null) {
			String action = request.getParameter("a");
			if (action != null) {
				if (action.equalsIgnoreCase("c"))
					create(request, response);
				else if (action.equalsIgnoreCase("r"))
					report(request, response);
				else if (action.equalsIgnoreCase("u"))
					update(request, response);
				else if (action.equalsIgnoreCase("d"))
					delete(request, response);
				else if (action.equalsIgnoreCase("l"))
					listAll(response);
				else if (action.equalsIgnoreCase("s"))
					listOrderStatus(response);
				else if (action.equalsIgnoreCase("p"))
					listPaymentStatus(response);
				else if (action.equalsIgnoreCase("oi"))
					listOrderItem(request, response);
				else if (action.equalsIgnoreCase("mi"))
					listMenuItem(response);
				else if (action.equalsIgnoreCase("ai"))
					addItem(request, response);
				else if (action.equalsIgnoreCase("ri"))
					removeItem(request, response);
				else if (action.equalsIgnoreCase("dq"))
					decreaseItemQty(request, response);
				else if (action.equalsIgnoreCase("si"))
					searchItem(request, response);
			} else
				response.setStatus(HttpServletResponse.SC_NO_CONTENT);
		} else
			response.setStatus(HttpServletResponse.SC_NO_CONTENT);
	}
	
	/* END (HttpServlet) Methods */

	
	/* Local Methods */
	
	
	private void create (HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession(true);
		PrintWriter out = response.getWriter();
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String phone = request.getParameter("phone");
		String status = request.getParameter("status");
		String paymentStatus = request.getParameter("paymentstatus");
		String delivery = request.getParameter("delivery");
		EmployeeAccountBean employee = (EmployeeAccountBean) session.getAttribute("employee");
		
		
		try {
			OrderControl ordCtrl = new OrderControl();
			OrderBean newOrder = ordCtrl.create(firstname, lastname, phone, status, paymentStatus, delivery, employee);
			out.write(getSuccess(Integer.toString(newOrder.getId())).toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void report (HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String phone = request.getParameter("phone");
		String status = request.getParameter("status");
		String paymentStatus = request.getParameter("paymentstatus");
		try {
			OrderControl ordCtrl = new OrderControl();
			List<OrderBean> list = ordCtrl.search(id, firstname, lastname, phone, status, paymentStatus);
			out.write(encapsulateBeanListXML(list, "orders"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
		
	}
	
	private void update (HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String phone = request.getParameter("phone");
		String status = request.getParameter("status");
		String paymentStatus = request.getParameter("paymentstatus");
		String delivery = request.getParameter("delivery");
		try {
			OrderControl ordCtrl = new OrderControl();
			ordCtrl.update(id, firstname, lastname, phone, status, paymentStatus, delivery);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
		
	}
	
	/**
	 * Passes the order ID to <code>OrderControl</code> to delete the order from the database.
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String id = request.getParameter("id");
		PrintWriter out = response.getWriter();
		try {
			OrderControl ordCtrl = new OrderControl();
			ordCtrl.delete(id);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	/**
	 * Receives the list of all the order from 
	 * <code>OrderControl</code> and sends it back to the front end.
	 * @param response
	 * @throws IOException
	 */
	private void listAll(HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		try {
			OrderControl ordCtrl = new OrderControl();
			List<OrderBean> list = ordCtrl.listAll();
			out.write(encapsulateBeanListXML(list, "orders"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	/**
	 * Receives the list of order status from <code>OrderControl</code> 
	 * and sends it back to the front end
	 * @param response
	 * @throws IOException
	 */
	private void listOrderStatus(HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			OrderControl ordCtrl = new OrderControl();
			List<String> list = ordCtrl.listOrderStatus();
			out.write(encapsulateStringListXML(list));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	/**
	 * Receives the list of payment status from <code>OrderControl</code> 
	 * and sends it back to the front end.
	 * @param response
	 * @throws IOException
	 */
	private void listPaymentStatus(HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			OrderControl ordCtrl = new OrderControl();
			List<String> list = ordCtrl.listPaymentStatus();
			out.write(encapsulateStringListXML(list));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	/**
	 * Receives the list of order items from <code>OrderControl</code>, that are in the order 
	 * with specified ID passed through POST method and sends it back to the front end.
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void listOrderItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			OrderControl ordCtrl = new OrderControl();
			List<OrderItemBean> list = ordCtrl.listOrderItem(id);
			out.write(encapsulateBeanListXML(list,"orderitems"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	/**
	 * Receives the list of all the menu items from <code>OrderControl</code> and 
	 * sends it back to the front end.
	 * @param response
	 * @throws IOException
	 */
	private void listMenuItem(HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			MenuItemControl miCtrl = new MenuItemControl();
			List<MenuItemBean> list = miCtrl.listAll();
			out.write(encapsulateBeanListXML(list,"menuitems"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void addItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String orderID = request.getParameter("orderid");
		String menuItemID = request.getParameter("menuitemid");
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			OrderControl ordCtrl = new OrderControl();
			ordCtrl.addItem(orderID, menuItemID);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void removeItem (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String orderID = request.getParameter("orderid");
		String menuItemID = request.getParameter("menuitemid");
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			OrderControl ordCtrl = new OrderControl();
			ordCtrl.removeItem(orderID, menuItemID);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void decreaseItemQty(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String orderID = request.getParameter("orderid");
		String menuItemID = request.getParameter("menuitemid");
		PrintWriter out = response.getWriter();
		response.setContentType("text/xml");
		try {
			OrderControl ordCtrl = new OrderControl();
			ordCtrl.decreaseItemQty(orderID, menuItemID);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void searchItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String name = request.getParameter("name");
		String number = request.getParameter("number");
		try {
			MenuItemControl miCtrl = new MenuItemControl();
			List<MenuItemBean> list = miCtrl.search(Integer.toString(0), number, name, Integer.toString(0));
			out.write(encapsulateBeanListXML(list, "menuitems"));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	/* END Local Methods */

}
